public without sharing class IntentionalProductMonthController {
    public String backId {get; private set;} 
	public String msg {get; private set;} 
	public Boolean isErr {get; private set;} 
	public String dataJson {get; private set;} 
	public String saveJson {get;  set;} 
	public Boolean isSuccess {get ; private set;}
	private String oppId ; 
    private String activityId;

    public IntentionalProductMonthController() {
         backId = System.currentPageReference().getParameters().get('id');
         isErr = false;
         activityId = null;
    }

    public void init(){

    	try {
    	    if(backId == null ) throw new DmlException('页面打开错误!');
    	    if(Id.valueOf(backId).getSobjecttype() == Opportunity.SObjecttype ){
    	    	oppId = backId;
    	    }else if(Id.valueOf(backId).getSobjecttype() == Event.SObjecttype ){
    	    	Event e = [select Id,Opportunity__c from Event where Id =:backId ];
    	    	if(e.Opportunity__c == null )throw new DmlException('事件关联的业务机会为空!');
    	    	oppId = e.Opportunity__c;
                activityId = String.valueOf(Id.valueOf(backId));

    	    }else {
    	    	throw new DmlException('页面打开错误!');
    	    }
    	    getDetails();
    	}
    	catch (Exception e) {
    	    isErr = true ;
    		ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR ,e.getMessage()));
    	}
    	
    }

    public void getDetails(){
    	List<IntentMonthBean> tmp = new List<IntentMonthBean>();

    	//查询已经存在的意向产品跟进
    	Set<Id> pest = new Set<Id>();
        String sql = 'select Id ,Elaborate__c ,Problems__c ,Plans__c ,ActivityID__c , IntentProduct__c,IntentProduct__r.Product__r.Name '+ 
            ' from IntentionalProductMonthFollow__c where ' +(activityId == null ? (' IntentProduct__r.Opportunity__c =\''+oppId+'\'' ) : (' ActivityID__c =\''+activityId+'\'')) ;
    	for (IntentionalProductMonthFollow__c ipm  : Database.query(sql)) {
    			
    		pest.add(ipm.IntentProduct__c);
    		tmp.add(new IntentMonthBean(ipm));
    	}
    	//将不存在跟进的意向产品找出来
    	for (IntentProduct__c ip : [select Id ,Product__r.Name 
    		from IntentProduct__c where Opportunity__c =:oppId and Product__r.Category0__c = '仪器' and  Id not in: pest   ] ) {
    		tmp.add(new IntentMonthBean(ip,activityId));

    	}
    	dataJson  = EncodingUtil.urlEncode(JSON.serialize(tmp),'UTF-8');

    }
    public void save(){

    	msg = '';
    	try {
    	    List<Object> saveList = (List<Object>)JSON.deserializeUntyped(EncodingUtil.urlDecode(saveJson, 'UTF-8'));

    	    List<IntentionalProductMonthFollow__c> ipmList = new List<IntentionalProductMonthFollow__c>();
    	    for (Object o  :saveList ) {
    	    	Map<String,Object> mo = (Map<String,Object>)o;
    	    	IntentionalProductMonthFollow__c ipm = new IntentionalProductMonthFollow__c();
    	    	ipm.Id = DataUtil.getStringValue(mo.get('id'));
    	    	if(ipm.Id == null ) ipm.IntentProduct__c  = DataUtil.getStringValue(mo.get('intentId'));
    	    	ipm.Plans__c = DataUtil.getStringValue(mo.get('plan'));
    	    	ipm.Problems__c = DataUtil.getStringValue(mo.get('problem'));
    	    	ipm.Elaborate__c = DataUtil.getStringValue(mo.get('descr'));
                ipm.ActivityID__c = DataUtil.getStringValue(mo.get('activityId'));

    	    	ipmList.add(ipm);
    	    }
    	    SavePoint sp = Database.setSavepoint();
    	    try {
		    	        
    	    	if(ipmList.size() > 0 ) upsert ipmList;
    	    	isSuccess = true;
    	        msg = '保存成功!';
    	    }
    	    catch (Exception e1) {
    	    	Database.rollback(sp);
    	  		msg = '保存失败:' +e1.getMessage();
    	    }
    	    
    	}
    	catch (Exception e) {
    	  	msg = '保存失败:' +e.getMessage();

    	}
    	


    }




    public class IntentMonthBean{

    	public String id; 
    	public String intentId ;
    	public String name ;
    	public String plan ; 
    	public String problem ;
    	public String descr ;
        public String activityId;

    	public  IntentMonthBean(IntentionalProductMonthFollow__c ipm ){
    		intentId  = ipm.IntentProduct__c;
    		name = ipm.IntentProduct__r.Product__r.Name ; 
    		plan = ipm.Plans__c;
    		problem = ipm.Problems__c;
    		descr = ipm.Elaborate__c ; 
    		id = ipm.Id;
            activityId = ipm.ActivityID__c;
    	}
    	public  IntentMonthBean(IntentProduct__c ipm ,String acId ){
    		intentId  = ipm.Id ;
    		name = ipm.Product__r.Name ; 
            activityId = acId;
    	
    	}
    }
}